# encoding: utf-8

#
#  BlogFolderView.py
#  AseanWeb
#
#  Created by nakanishi on 09/04/23.
#  Copyright (c) 2009 CMScom. All rights reserved.
#

from zope.component import getMultiAdapter
from Products.Five.browser import BrowserView
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from zope.interface import implements
from Acquisition import aq_inner
from Products.CMFCore.utils import getToolByName
from DateTime import DateTime

class BlogFolderView(BrowserView):
    __call__ = ViewPageTemplateFile('BlogFolderView.pt')
    
    def isAnon(self):
        portal_state = getMultiAdapter((self.context, self.request),
                                        name=u'plone_portal_state')
        return portal_state.anonymous()
    
    def trans_pub_date(self,time):
        """get pub date"""
        tool = getToolByName(self, 'portal_languages', None)
        bound = tool.getLanguageBindings()
        lang = bound[0]
        if not isinstance(time, DateTime):
            time=DateTime(time)
        if lang == 'ja':
            vTimeStr = '%s年%s月%s日' % (time.year(),time.month(),time.day())
        else:
            vTimeStr = '%s-%s-%s' % (time.year(),time.month(),time.day())
        return vTimeStr
    
    def getEntries(self, vType='', vYear=0):
        #import pdb;pdb.set_trace()
        context = self.context
        path = self.context.getPhysicalPath()
        cur_path = '/'.join(path)
        base_query = {}
        base_query['sort_on'] = 'pub_date'
        base_query['sort_order'] = 'reverse'
        base_query['path'] = {'query':cur_path, 'depth':1}
        if vType:
            base_query['getBlogCategory'] = vType
        base_query['portal_type'] = ('BlogEntry',)
        if (vYear != 0):
            st_data = DateTime('%s/01/01' % (vYear))
            ed_data = DateTime('%s/12/31 23:59' % (vYear))
            base_query['pub_date'] = {'query':[st_data,ed_data], 'range':'minmax'}
        portal_catalog = getToolByName(self, 'portal_catalog')
        return portal_catalog(base_query)
    
    